package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.os.Build;
import android.util.Log;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.MeterLog;

import java.time.LocalDateTime;

public class MeterLogMapper {
    public static final String TABLE = "meter_log";
    public static final String CREATE_TABLE_SQL="CREATE TABLE "+ TABLE +"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "device_code TEXT," +
            "data_time TEXT," +
            "sr_type TEXT," +
            "data_size INTEGER," +
            "content TEXT," +
            "username TEXT" +
            ");";

    private static final String TAG = "MeterLogMapper";
    private  static final String SR_TYPE_S = "snd";
    private  static final String SR_TYPE_R = "rcv";


    public static void delete(String device_code){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() + "' and device_code= '"+device_code+"'",null);
    }
    public static void delete(){
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username='" + HesAppStatic.getUsername() +"'",null);
    }
    public static void insert(MeterLog meterLog){
        ContentValues contentValues = new ContentValues();
        contentValues.put("device_code", meterLog.getDeviceCode());
        contentValues.put("content", meterLog.getContent());
        contentValues.put("data_time", meterLog.getDataTime());
        contentValues.put("data_size", meterLog.getDataSize());
        contentValues.put("sr_type", meterLog.getSrType());
        contentValues.put("username", meterLog.getUsername());
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE,contentValues);
        Log.d(TAG, "(LOG收发数据) —— " + meterLog.getDataTime()
                + " |" + meterLog.getDeviceCode()
                + "| " + meterLog.getDataTime()
                + " |<" + meterLog.getDataSize()
                + ">| " + meterLog.getContent());
    }

    public static void insert(String deviceCode, int datasize, String content, String srType){
        MeterLog meterLog = new MeterLog();
        meterLog.setContent(content);
        meterLog.setDataSize(datasize);
        meterLog.setDeviceCode(deviceCode);
        meterLog.setSrType(srType);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            meterLog.setDataTime(LocalDateTime.now().toString());
        }
        insert(meterLog);
    }

    public static void insertSend(String deviceCode, int dataSize, String content){
        insert(deviceCode, dataSize, content, SR_TYPE_S);
    }

    public static void insertRcv(String deviceCode, int dataSize, String content){
        insert(deviceCode, dataSize, content, SR_TYPE_R);
    }
}
